######################################################################################
#
# This file was provided as part of the SharePoint Mystery Deployment package
# Created by Christopher Kimbell (http://sharepointmystery.com)
#
######################################################################################

# -----------------------------------------------------------------
# This file represents a sample command dispatcher. 
# This file determines what each command from the command line actually does.
# Customize based on your topology needs.
# At the end of the file you will find the mapping between applications and command handler
#
# -----------------------------------------------------------------


# -----------------------------------------------------------------
# Handles commands for an application
# -----------------------------------------------------------------
function RunApplicationCmd($cmd, $application)
{
	$application.Validate()
	$application.SearchBox.Validate()
	
	switch($application.ApplicationType)
	{
		"MySite"  { $userProfileSvc.Validate(); break; }
		"Support" { $metadataSvc.Validate(); break; }
	}
	
    switch($cmd)
    {
		"PrepareServers"
		{
			$application.PrepareServers() 
			break;
		}
        "Provision"
        {   
			$application.PrepareServers() 
            $application.Provision() 
            $application.DeploySolutions()
            $application.ActivateFeatures()
			$application.SC.Provision()
			$application.SC.ActivateFeatures()
            $application.SC.ConfigureAccess()
            $application.SearchBox.Configure()
			
			$application.InternetZone.CreateHttpHttpsMapping()
			#$application.InternetZone.Provision()
			
			switch($application.ApplicationType)
			{
				"MySite"
				{
					$userProfileSvc.Provision()
					break;
				}
				"Support"
				{
					$metadataSvc.Hub.Provision()
					# forces the timer job to publish content types
					Start-SPTimerJob -Identity "MetadataHubTimerJob" -Confirm:$false 
					break;
				}
			}
            break;
        }
        "PreUpgrade"
        {
            $application.RetractSolutions();
            break;
        }
        "Upgrade"
        {
			# this is not run in the PreUpgrade step since we don't want any cached .NET code from the old version
			# to screw up things
            $application.DeploySolutions()
            break;
        }
        "PostUpgrade"
        {
			# this is not run in the Upgrade step since deployinf solutions add stuff we may want to use
			# if running in same session, SharePoint caching may prevent us from accessing things
            $application.ActivateFeatures()
            $application.UpgradeFeatures()
            break;
        }
        "Unprovision"
        {
            $application.RetractSolutions();
            $application.Unprovision()
            break;
        }
        default
        {
           Terminate "Unknown Command: $($cmd)"
        }
    }
}

# -----------------------------------------------------------------
# Handles commands for various services
# -----------------------------------------------------------------
function RunServicesCommand($cmd)
{
    $accessSvc.Validate()
    $bcsSvc.Validate()
    $excelSvc.Validate()
    $metadataSvc.Validate()
    $ppSvc.Validate()
    $pptSvc.Validate()
    $searchSvc.Validate()
    $secureStoreSvc.Validate()
    $stateSvc.Validate()
    $usageSvc.Validate()
    $userProfileSvc.Validate()
    $visioSvc.Validate()
    $analyticsSvc.Validate()
    $wordAutomationSvc.Validate()
    $wordViewingSvc.Validate()

    switch($cmd)
    {
        "Provision"
        {
			$searchSvc.Provision()
            $accessSvc.Provision()
            $bcsSvc.Provision() 
            $excelSvc.Provision()
            $metadataSvc.Provision()
            $ppSvc.Provision()
            $pptSvc.Provision()
            $secureStoreSvc.Provision()
            $stateSvc.Provision()
            $usageSvc.Provision()
            #user profile is intentionally left out since it depends on the mysite web application
			$visioSvc.Provision()
            $analyticsSvc.Provision()
            $wordAutomationSvc.Provision()
            $wordViewingSvc.Provision()
            break;
        }
        "Unprovision"
        {
			$searchSvc.Unprovision()
            $accessSvc.Unprovision()
            $bcsSvc.Unprovision()
            $excelSvc.Unprovision()
            $metadataSvc.Unprovision()
            $ppSvc.Unprovision()
            $pptSvc.Unprovision()
            $secureStoreSvc.Unprovision()
            $stateSvc.Unprovision()
            $usageSvc.Unprovision()
            $userProfileSvc.Unprovision()
            $visioSvc.Unprovision()
            $analyticsSvc.Unprovision()
            $wordAutomationSvc.Unprovision()
            $wordViewingSvc.Unprovision()
            break;
        }
    }
}

function RunFarmCommand($cmd)
{	
    switch($cmd)
    {
		"PrepareServers"
		{
			$farm.PrepareServers()
			break;
		}
		"DeploySolution"
		{
			$farm.DeploySolutions()
			break;
		}
		"Configure"
        {
            #.\RegisterEventSources.exe
			$farm.Configure()  
            break;
        }
		"RegisterAccounts"
		{
			Write-Operation "Registering Managed Accounts"
			$ma = $mySiteApp.Pool.RegisterAccount()
			$ma = $supportApp.Pool.RegisterAccount()
			$ma = $publishingApp.Pool.RegisterAccount()
			$ma = $collaborationApp.Pool.RegisterAccount()
			$ma = $searchApp.Pool.RegisterAccount()
			$ma = $recordCenterApp.Pool.RegisterAccount()

			$ma = $accessSvc.Pool.RegisterAccount()
			$ma = $bcsSvc.Pool.RegisterAccount()
			$ma = $excelSvc.Pool.RegisterAccount()
			$ma = $metadataSvc.Pool.RegisterAccount()
			$ma = $ppSvc.Pool.RegisterAccount()
			$ma = $pptSvc.Pool.RegisterAccount()
			$ma = $searchSvc.Pool.RegisterAccount()
			$ma = $searchSvc.RegisterAccounts()
			$ma = $secureStoreSvc.Pool.RegisterAccount()
			$ma = $userProfileSvc.Pool.RegisterAccount()
			$ma = $visioSvc.Pool.RegisterAccount()
			$ma = $analyticsSvc.Pool.RegisterAccount()
			$ma = $wordAutomationSvc.Pool.RegisterAccount()
			$ma = $wordViewingSvc.Pool.RegisterAccount()
			break;
		}
		"PreUpgrade"
        {
            $farm.RetractSolutions()
            break;
        }
        "Upgrade"
        {
            $farm.DeploySolutions()
            break;
        }
        "PostUpgrade"
        {
            break;
        }
    }
}

# -----------------------------------------------------------------
# This is the application dispatcher that calls the appropriate
# handler for each application
# -----------------------------------------------------------------

$logFile = "_" + $currentCmd + "_" + $currentApp + ".log"
Start-Transcript $logFile

# make sure all values have been defined
$farm.Validate()

switch($currentApp)
{
    "Farm" 
	{ 
		RunFarmCommand($currentCmd); 
		break; 
	}
    "MySite" 
	{ 
		RunApplicationCmd $currentCmd $mySiteApp; 
		break; 
	}
    "Collaboration" 
	{ 
		RunApplicationCmd $currentCmd $collaborationApp 
		break; 
	}
    "RecordCenter" 
	{ 
		RunApplicationCmd $currentCmd $recordCenterApp 
		break; 
	}
    "Publishing" 
	{ 
		RunApplicationCmd $currentCmd $publishingApp 
		break; 
	}
    "Support" 
	{ 
		RunApplicationCmd $currentCmd $supportApp
		break; 
	}
    "Search" 
	{ 
		RunApplicationCmd $currentCmd $searchApp
		break; 
	}
    "Services" 
	{ 
		RunServicesCommand($currentCmd); 
		break; 
	}
}

Stop-Transcript